package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.ExpenseDetail;
import com.woniu.mapper.ExpenseDetailMapper;
import com.woniu.service.IExpenseDetailService;
import com.woniu.util.EmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
@Service
@Transactional
@Slf4j
public class ExpenseDetailServiceImpl extends ServiceImpl<ExpenseDetailMapper, ExpenseDetail> implements IExpenseDetailService {
    @Autowired
    private ExpenseDetailMapper em;


    //全查
    @Override
    public List<ExpenseDetail> getAll() {
        return em.getAll();
    }



    //根据报销单号查询
    //EDID -->exp_detail_id
    @Override
    public List<ExpenseDetail> getByEDId(int edid) {
        return em.getByEDId(edid);
    }



    //根据报销类别号查询
    //EID -->exp_id
    @Override
    public List<ExpenseDetail> getByEid(int eid) {
        return em.getByEId(eid);
    }



    //根据银行账号查询
    //BID -->bank_id
    @Override
    public List<ExpenseDetail> getByBid(int bid) {
        return em.getByBId(bid);
    }


    //根据部门ID查询
    //DID -->dept_id
    @Override
    public List<ExpenseDetail> getByDid(int did) {
        return em.getByDId(did);
    }


    //根据报销时间查询支持模糊查询
    //EDate -->exp_date
    @Override
    public List<ExpenseDetail> getByEDate(String edate) {
        return em.getByEDate(edate);
    }


    //添加报销单
    //EDetail-->Expense_Detail
    //报销状态为三种，r-->初次提交报销；y-->审批通过；n-->审批未通过
    @Override
    public void addEDetail(ExpenseDetail eDetail) {
//        if (eDetail.getExpDetailId()>0 && eDetail.getExpId()>0 && eDetail.getBankId()>0 && eDetail.getDeptId()>0  && !"".equals(eDetail.getExpDate()) && eDetail.getExpPrice()==null && eDetail.getExpCredential()==null ){
//            em.addEDetail(eDetail);
//        }
        em.addEDetail(eDetail);
    }


    //审批（未通过）
    @Override
    public void unpass(int edid, String efback) {
        if (!"".equals(efback)){
            em.unpass(edid,efback);
        }
    }

    @Override
    public void pass(int edid,String efback) {
        if ("".equals(efback)){
            efback ="同意报销";
        }
        em.pass(edid,efback);
    }

    //条件判断
    private static QueryWrapper getExpWrapper(ExpenseDetail expenseDetail) {
        QueryWrapper<ExpenseDetail> wrapper = new QueryWrapper<>();
        if (EmptyUtil.isEmpty(expenseDetail)) {
            wrapper.eq("1","1");
            return wrapper;
        }
        if (EmptyUtil.isNoEmpty(expenseDetail.getExpense())&&EmptyUtil.isNoEmpty(expenseDetail.getExpense().getExpId())){
            wrapper.eq("expense_detail.exp_id",expenseDetail.getExpense().getExpId().toString());
        }
        if (EmptyUtil.isNoEmpty(expenseDetail.getBankId())){
            wrapper.eq("expense_detail.bank_id",expenseDetail.getBankId());
        }
        if (EmptyUtil.isNoEmpty(expenseDetail.getDept())&&EmptyUtil.isNoEmpty(expenseDetail.getDept().getDeptId())){
            wrapper.eq("expense_detail.dept_id",expenseDetail.getDept().getDeptId());
        }
        if (EmptyUtil.isNoEmpty(expenseDetail.getExpDetailStatus())){
            wrapper.eq("expense_detail.exp_detail_status",expenseDetail.getExpDetailStatus());
        }
        if (EmptyUtil.isNoEmpty(expenseDetail.getExpStartTime())){
            wrapper.gt("exp_date",expenseDetail.getExpStartTime());
        }
        if (EmptyUtil.isNoEmpty(expenseDetail.getExpEndTime())){
            wrapper.lt("exp_date",expenseDetail.getExpEndTime());
        }
        System.out.println(wrapper.getSqlSelect());
        System.out.println(wrapper.getCustomSqlSegment());
        System.out.println(wrapper.getSqlSegment());
        return wrapper;
    }

    //多条件查询
    @Override
    public List<ExpenseDetail> getByCondition(ExpenseDetail expenseDetail) {
        QueryWrapper<ExpenseDetail> wrapper = getExpWrapper(expenseDetail);
        return em.getByCondition(wrapper);
    }

    //多条件分页查询
    @Override
    public Page<ExpenseDetail> getByConditionWithPage(Integer p, Integer pageSize, ExpenseDetail expenseDetail) {
        QueryWrapper<ExpenseDetail> wrapper = getExpWrapper(expenseDetail);
        Page<ExpenseDetail> page = new Page<>(p,pageSize);
        return em.getByConditionWithPage(page,wrapper);
    }

    @Override
    public Page<ExpenseDetail> myListWithPage(Integer p, Integer pageSize,Integer uid) {
        Page<ExpenseDetail> page = new Page<>(p,pageSize);
        return em.myListWithPage(page,uid);
    }


}
